import java.util.Scanner;
import java.util.Stack;
 class ListNode {
    int val;
    ListNode next = null;
    public ListNode(int val) {
      this.val = val;
    }
  }
public class Demo3 {
    public static ListNode addInList (ListNode head1, ListNode head2) {
        // write code here
        head1 = transnation(head1);
        head2 = transnation(head2);
        ListNode pret = new ListNode(0);
        ListNode ret = pret;

        ListNode pHead1 =head1, pHead2 =head2;
        while(pHead1 != null && pHead2 != null) {
            int temp = pHead1.val + pHead2.val;
            if(temp >= 10) {
                pret.val += temp -10;
                pret.next = new ListNode(1);
            } else {
                pret.val += temp;
                pret.next = new ListNode(0);
                if(pret.val >= 10) {
                    pret.val = 0;
                    pret.next.val = 1;
                }

            }
            pret = pret.next;
            pHead1 = pHead1.next;
            pHead2 = pHead2.next;
        }
        while(pHead1 != null) {
            pret.val += pHead1.val;
            pHead1 = pHead1.next;
            pret.next = new ListNode(0);
            pret = pret.next;
        }
        while(pHead2 != null) {
            pret.val += pHead2.val;
            pHead1 = pHead2.next;
            pret.next = new ListNode(0);

            pret = pret.next;
        }
        ret =  transnation(ret);
        if(ret.val >= 10) {
            ret.val -= 10;
            ListNode x = new ListNode(1);
            x.next = ret;
            return x;
        }
        return ret;
    }
    //转置
    private static ListNode transnation(ListNode head1) {
        ListNode cur = head1.next;
        ListNode pre = head1;
        pre.next = null;
        ListNode next = head1;
        while(cur != null) {

            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
            if(next != null) {
                next = next.next;
            }
        }
        return pre;
    }
    public static void main(String[] args) {
        ListNode head1 = new ListNode(9);
        ListNode temp0 = new ListNode(9);

        ListNode temp1 = new ListNode(3);
        ListNode temp2 = new ListNode(7);

        head1.next = temp0;
        head1.next.next = temp1;
        head1.next.next.next = temp2;

        ListNode head2 = new ListNode(6);
        ListNode temp3 = new ListNode(3);

        head2.next = temp3;
        addInList(head1,head2);




        /*Scanner scanner = new Scanner(System.in);
        String s,t;
        String strs = scanner.next();
        String[] temp = strs.split(",");
        s = temp[0];
        t = temp[1];

        int ret = number(s) * number(t);
        StringBuffer st = new StringBuffer("\"");
        st.append(ret);
        st.append("\"");
        System.out.println(st.toString());


    }
    private static int number(String s) {
        Stack<Character> stack= new Stack<>();
        for(int i = 1; i < s.length()-1; i++) {
            stack.add(s.charAt(i));
        }
        int ret = 0;
        for(int i = 1; i < s.length()-1; i++) {
            ret += ( ( (int)stack.pop()-'0' ) * Math.pow(10,i-1) );
        }
        return ret;*/
    }
}

